#include "gtest/gtest.h"
#include "entitymanager.h"
#include "Entities/dummy.h"
#include <Entities/plant.h>
#include <memory>
#include <Skills/link.h>

using namespace std;

TEST(plantTest, link_manager_test) {
    unique_ptr<Game> g(new Game());
    Entity *dummy = g->entityManager.get<entity::Dummy>();
    Actor *dummy1 = g->entityManager.get<entity::Dummy>();
    //entity::EnergyManager* lm = g->entityManager.get<entity::EnergyManager>();
    dummy->learnSkill(new skill::Link);
    dummy->learnSkill(new skill::Connect);
    dummy->learnSkill(new skill::Transfer);
    dummy->properties["energy"] = to_string(50);
    dummy->properties["transport_capacity"] = to_string(50);
    dummy1->properties["energy"] = to_string(50);
    dummy->properties["link_out"] = "|";
    dummy1->properties["link_in"] = "|";
    dummy->properties["outdeg"] = "3";
    //g->linkManager = lm;
    // dummy->useSkill("Link", {dummy->id, dummy1->id});
    dummy->useSkill("Connect", {dummy1->id});
    g->oneRoundRun();
    EXPECT_EQ(to_string(dummy->hasSkill("LinktimeBuff"+dummy1->id)),"1");
    g->oneRoundRun();
    EXPECT_EQ(to_string(dummy->hasSkill("LinktimeBuff"+dummy1->id)),"1");
    g->oneRoundRun();
    EXPECT_EQ(to_string(dummy->hasSkill("LinktimeBuff"+dummy1->id)),"1");
    g->oneRoundRun();

    EXPECT_EQ(to_string(dummy->hasSkill("LinktimeBuff"+dummy1->id)),"0");
    dummy->useLater("Transfer", {dummy1->id,to_string(10)});
    g->oneRoundRun();
    EXPECT_EQ(dummy->properties["energy"], to_string(40));
    EXPECT_EQ(dummy1->properties["energy"], to_string(60));
    g->dumpReplay("LinkTest");
}

TEST(plantTest, autoDisconnect){

    unique_ptr<Game> g(new Game());
    Actor *p1 = g->entityManager.get<entity::TestPlant>();
    Actor *p2 = g->entityManager.get<entity::TestPlant>();
    p1->useSkill("Connect", {p2->id});
    p2->useSkill("Connect", {p1->id});
    for (unsigned i = 0; i < 4; ++ i)
        g->oneRoundRun();
    p1->properties["hp"] = to_string(-1000);
    p2->properties["hp"] = to_string(-1000);
    g->oneRoundRun();
    g->dumpReplay("autoDisconnect");
}

TEST(plantTest, ConnectRange){

    unique_ptr<Game> g(new Game());
    Actor *p1 = g->entityManager.get<entity::TestPlant>();
    Actor *p2 = g->entityManager.get<entity::TestPlant>();
    p1->setPos({1, 2});
    p2->setPos({1, 9});
    p1->useSkill("Connect", {p2->id});
    p2->useSkill("Connect", {p1->id});
    g->oneRoundRun();
    g->dumpReplay("ConnectRange");
}